package grammar.parser.stmt;

import error.processing.MyErrorProcessing;
import exp.parser.Exp;
import grammar.parser.BlockItem;
import word.parser.WordElement;

public abstract class Stmt extends BlockItem {

    public static Stmt buildStmt() {
        WordElement.WordType type = getAnalyzingWord().getType();
        switch (type) {
            case SEMICN:
                analyzeNextWord();
                return new ExpStmt(null);
            case LBRACE:
                return Block.buildBlock();
            case IFTK:
                return IfStmt.buildIfStmt();
            case WHILETK:
                return WhileStmt.buildWhileStmt();
            case BREAKTK:
            case CONTINUETK:
                return BreakContinueStmt.buildBreakContinueStmt();
            case RETURNTK:
                return ReturnStmt.buildReturnStmt();
            case PRINTFTK:
                return PrintfStmt.buildPrintStmt();
            default:
                Exp exp = Exp.buildExp();
                if (getAnalyzingWord().getType() == WordElement.WordType.ASSIGN) {
                    return Assign.buildAssignStmt(exp.toLVal());
                } else {
                    if (getAnalyzingWord().getType() != WordElement.WordType.SEMICN) {
                        MyErrorProcessing.buildErrorProcessing(MyErrorProcessing.ErrorType.i,
                                getAnalyzingWord(-1));
                    } else {
                        analyzeNextWord();
                    }
                    return new ExpStmt(exp);
                }
        }
    }
}
